# 简易画图，定死了数据情况下，画图（单线程下）三种设备对两种类型算子的亲和度对比（运行时间）。

import numpy as np
import matplotlib.pyplot as plt

# 设置中文字体支持
# plt.rcParams['font.sans-serif'] = ['SimHei']  # 使用黑体
plt.rcParams['axes.unicode_minus'] = False    # 正确显示负号

# 数据准备
operators = ['fillnull', 'sigrid_hash']
devices = ['ARM', 'CPU', 'DPA']

# p50数据 (50次平均时间)
p50_data = {
    'fillnull': {
        'ARM': 5.686,
        'CPU': 4.585,
        'DPA': 298
    },
    'sigrid_hash': {
        'ARM': 35.035,
        'CPU': 9.245,
        'DPA': 330.754
    }
}

# p99数据 (最长运行时间)
p99_data = {
    'fillnull': {
        'ARM': 18.640,
        'CPU': 17.227,
        'DPA': 298.04
    },
    'sigrid_hash': {
        'ARM': 47.195,
        'CPU': 21.984,
        'DPA': 330.754
    }
}

# 创建两个图表：一个用于P50，一个用于P99
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 6))

# 设置柱状图宽度和位置
bar_width = 0.25
x = np.arange(len(operators))

# 颜色设置
colors = {'ARM': 'tab:blue', 'CPU': 'tab:green', 'DPA': 'tab:red'}

# 绘制P50图表
for i, device in enumerate(devices):
    # 获取该设备在所有算子上的p50值
    p50_vals = [p50_data[op][device] for op in operators]
    
    # 绘制柱状图
    offset = i * bar_width
    bars = ax1.bar(x + offset, p50_vals, bar_width, 
                  label=device, color=colors[device])
    
    # 添加数值标签
    for bar in bars:
        height = bar.get_height()
        ax1.text(bar.get_x() + bar.get_width()/2, height + 5, 
                f'{height:.1f}ms', ha='center', va='bottom', fontsize=10)

# 设置P50图表
ax1.set_title('P50 Performance (50_times_avg)', fontsize=14)
ax1.set_ylabel('run_time (ms)', fontsize=12)
ax1.set_xticks(x + bar_width)
ax1.set_xticklabels(operators, fontsize=12)
ax1.legend(fontsize=10)
ax1.grid(axis='y', linestyle='--', alpha=0.7)

# 绘制P99图表
for i, device in enumerate(devices):
    # 获取该设备在所有算子上的p99值
    p99_vals = [p99_data[op][device] for op in operators]
    
    # 绘制柱状图
    offset = i * bar_width
    bars = ax2.bar(x + offset, p99_vals, bar_width, 
                  label=device, color=colors[device])
    
    # 添加数值标签
    for bar in bars:
        height = bar.get_height()
        ax2.text(bar.get_x() + bar.get_width()/2, height + 5, 
                f'{height:.1f}ms', ha='center', va='bottom', fontsize=10)

# 设置P99图表
ax2.set_title('P99 Performance (first_run)', fontsize=14)
ax2.set_ylabel('run_time (ms)', fontsize=12)
ax2.set_xticks(x + bar_width)
ax2.set_xticklabels(operators, fontsize=12)
ax2.legend(fontsize=10)
ax2.grid(axis='y', linestyle='--', alpha=0.7)

# 添加总标题
plt.suptitle('device_performance_for_different_operator (thread_num=1, batch_size=4194304(32M) )', fontsize=16, y=1.05)

# 调整布局
plt.tight_layout()

# 保存图像
plt.savefig('../results_raw/operator_performance_comparison.png', dpi=300, bbox_inches='tight')

# 显示图像
plt.show()
